package Thread;

import Ehcache.EhcacheManager;
import net.sf.ehcache.Cache;

/**
 * @author: lqh
 * @description: 三个售票窗口同时出售20张票;
 * @program: Demo
 * @create: 2018-04-12 16:32
 **/
public class Station implements Runnable {
    //由于票是三个站台共用的，所以为static
    public static Integer ticketCount = 20;

    public String window;

    Thread thread = null;

    public Station(String window) {
        this.window = window;
    }

    public void startSale() {
        if (thread == null) {
            thread = new Thread(this, window);
        }
        thread.start();
    }

    @Override
    public void run() {
        while (ticketCount > 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (ticketCount) {  //同步锁，确保一张票只能卖一次
                if (ticketCount > 0) {
                    System.out.println("<" + window + ">成功售出:" + ticketCount + "号票");
                    ticketCount--;
                } else {
                    System.out.println("车票已经售完");
                }
            }
        }
    }


    public static void main(String[] args) {
        Station one = new Station(" one ");
        Station twe = new Station(" twe ");
        Station three = new Station("three");
        one.startSale();
        twe.startSale();
        three.startSale();
    }
}
